package com.lqg.bookLibrary.controller;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.lqg.bookLibrary.common.constant.HostParam;
import com.lqg.bookLibrary.common.core.domain.JsonResult;
import com.lqg.bookLibrary.common.myInterface.CurrentUser;
import com.lqg.bookLibrary.common.myInterface.CurrentUserPojo;
import com.lqg.bookLibrary.pojo.Users;
import com.lqg.bookLibrary.service.IUsersService;
import com.lqg.bookLibrary.service.MailService;
import com.lqg.bookLibrary.service.webService.FileService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/user")
@CrossOrigin
public class UserController {

    @Autowired
    private IUsersService usersService;

    @Autowired
    private PasswordEncoder passwordEncoder;

    @Autowired
    private FileService fileService;

    @GetMapping("/detail")
    public JsonResult getUserInfoDetail(@CurrentUser CurrentUserPojo userPojo) {
        Long userId = userPojo.getUserId();
        Users one = usersService.lambdaQuery().eq(Users::getUserId, userId).one();
        return JsonResult.success(one);
    }

    @GetMapping("/info")
    public JsonResult getUserInfo(@CurrentUser CurrentUserPojo userPojo) {
        Long userId = userPojo.getUserId();
        Users one = usersService.lambdaQuery().eq(Users::getUserId, userId).one();
        Map<String,Object> map = new HashMap<>();
        map.put("id",one.getUserId());
        map.put("username",one.getUsername());
        JsonResult result = JsonResult.success(map);
        return result;
    }

    @PostMapping("/update")
    public JsonResult userUpdate(@CurrentUser CurrentUserPojo userPojo,
                                 @RequestBody String post
                                 ) {
        Long userId = userPojo.getUserId();
        Users users = usersService.lambdaQuery().eq(Users::getUserId, userId).one();
        JSONObject keyValue = JSON.parseObject(post);
        String username =(String)keyValue.get("username");
        String password =(String)keyValue.get("password");
        String photo =(String)keyValue.get("photo");
        String phone =(String)keyValue.get("phone");
        String email =(String)keyValue.get("email");
        users.setUsername(username);
        if (!password.equals(users.getPassword())) {
            users.setPassword(passwordEncoder.encode(password));
        }
        users.setPhoto(photo);
        users.setPhone(phone);
        users.setEmail(email);
        boolean b = usersService.updateById(users);
        if (b) {
            return JsonResult.success(users);
        }else {
            return JsonResult.fail("更新失败");
        }
    }


    @PostMapping("/avatar/upload")
    public JsonResult uploadAvatar(@RequestParam("avatar") MultipartFile zipFile,
                                   @CurrentUser CurrentUserPojo userPojo){
        Long userId = userPojo.getUserId();
        // 文件保存
        String filePath = HostParam.MAIN_PATH + "/user/avatar/";
        String fileName = userId +"_avatar.jpg";
        String fwUri = HostParam.ACCESS_PATH + "/user/avatar/" +fileName;
        fileService.upLoadFile(zipFile, filePath, fileName);
        return JsonResult.success("success",fwUri);
    }


}
